<?php

/**
 * display picasa photos on your website
 *
 * @author		pieter van peteghem <pieter@onedotonly.be>
 * @copyright	oneDot only <we@onedotonly.be> Copyright (c) 2010, oneDot only. All rights reserved.
 */
 

class SocialPicasa extends MOD {

	function __construct( $vars ) {
		$this->_addvars( $vars );
		$this->_ref();
	}
	
	function output() {
		
		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();

		// add css & show variables
		if ( $result ) {
			$this->addclass( $this->action );
		}

		return $this->parse( $result ) . $this->render_js();
		
	}
	
	
	function filter_album() {
	
		$ch = curl_init("https://graph.facebook.com/oauth/exchange_sessions");
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, array(
			"type" => "client_cred",
			"client_id" => "110430572343111",
			"client_secret" => "8ddc11e7c9a9661c01938393e307b029",
			"sessions" => $_REQUEST["fb_sig_session_key"]
		));
		
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$result = json_decode(curl_exec($ch));
		$accessToken = $result[0]->access_token;
			
		print_r($result);	
			
		return $accessToken;	
			
	}
	
	
	function filter_album2() {
	
		$sources = array('picasa', 'flickr');
		
		$select = '<span class="left"><select name="source">';
		foreach ($sources as $source) {
			$select .= '<option value="' . $source . '">' . $source . '</option>';
		}
		$select .= '</select></span>';
		$select .= '<input type="text" name="id" />';
		$select .= '<input type="button" class="add" name="add" value="voeg album toe" />';
		
		$this->js = "
			mod = new SocialCommunication( '" . json_encode( $this->vars ) . "' );
			mod.add();
		";
		
		return $this->render_form( $select );
	
	}
	
		
	function render_youtube( $id, $width, $height ) {
		
		$movie 	= $this->render_movie( 'irJry79_lAM', 'youtube', 254, 197 );
		//$movie 	= $this->render_movie( $id, 'youtube', $width, $height );
		return $movie;
	
	}
	
	
	function render_vimeo( $id, $width, $height ) {
		
		//$movie 	= $this->render_movie( '6505158', 'vimeo', 250, 195 );
		$movie 	= $this->render_movie( $id, 'vimeo', $width, $height );
		return $movie;
	
	}
	
	/*
	 *
	 * render movie maakt het mogelijk lijsten filmpjes van gebruikers te gaan afspelen, elk met hun eigen source
	 * foreach ($movies (lijst van alle filmpjes van een gebruiker) as $movie) {
	 	render_movie(...)
	 */	
	 
	function filter_youtube() {
	
		$movie 	= $this->render_movie( 'irJry79_lAM', 'youtube', 254, 180 );
		//$movie 	= $this->render_movie( $id, 'youtube', $width, $height );
		return $movie;
	}

	 
	
	function filter_flickr_pictures() {
	
		$Flickr = new Flickr( '7ddb581ea9a5a2e822b6bb3d1d672066' );
		$photos = $Flickr->getAlbum( '72157619415192530', array( 'ownername', 'photo' ) );
		
		$return = $this->render_list( $photos['photo']['url'], 'photo', true);
		return $return;
	
	}
	
	
	function filter_picasa_pictures() {

		$Picasa = new Picasa();
		$photos = $Picasa->pictures( 'canatuur2007', '5149854089904202769', $this->vars->sze );
		
		$return = $this->render_list( $photos, 'photo', $this->vars->limit, true );
		return $return;
		
	}


	/**
	 * render list
	 * 
	 * @return		string
	 * @param		array $nodes		all list items
	 * @param		string $class		add extra class
	 */
	
	function render_list($nodes, $class = 'list_item', $limit = false, $img = false ) {
	
		$res  = '<ul>';
		$i = 0;
		foreach ($nodes as $node) {
			if ( $img )
				$res .= '<li class="' . $class .'"><img src="' . $node . '"/></li>';
			else 
				$res .= '<li class="' . $class .'">' . $node . '</li>';
				
			$i++;
			if ( $limit && $i == (int) $limit ) break;
		}
		$res .= '</ul>';
		
		return $res;
	} 
	
	
	// get link of movie
	function render_link( $id, $source ) {
	
		if ($source == 'youtube') {
			$url	= 'http://www.youtube.com/v/' . $id . '&hl=en_US&fs=1&';
		} else if ($source == 'vimeo') {
			$url	= 'http://vimeo.com/moogaloop.swf?clip_id=' . $id . '&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1"';
		}
		
		return $url;
	}
	
	
	// render movie
	function render_movie( $id, $source, $width = 480, $height = 385 ) {
	
		$url	= $this->render_link( $id, $source );
		
		$movie	= '<object width="' . $width . '" height="' . $height . '"><param name="movie" value="' . $url . '"></param>';
		$movie	.= '<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>';
		$movie	.= '<embed src="' . $url . '" type="application/x-shockwave-flash" 
			allowscriptaccess="always" allowfullscreen="true" width="' . $width . '" height="' . $height . '"></embed></object>';
			
		return $movie;
	}
}



/** 
 * FLICKR CLASS
 *
 * This can be used to communicate with flickr api
 * This is only the beginning of a flickr communication.. should be expanded in the future!
 *
 * @author		Pieter Van Peteghem <pieter@onedotonly.be>
 * @version		version 0.01 beta
 *
 * @copyright	oneDot only <we@onedotonly.be> Copyright (c) 2010, oneDot only. All rights reserved.
 * @license		BSD License
 */

class Flickr {

	CONST FLICKR_URL 	= 'http://api.flickr.com/services/rest/';

	
	/**
	 * Default constructor
	 *
	 * @return	void
	 * @param	string $apikey			The consumer key to use.
	 */
	 
	public function __construct( $apikey ) {
		$this->apikey = $apikey;
	}
	
	
	/**
	 * get album with info
	 *
	 * @return	array 
	 * @param 	user
	 * @param 	albumid
	 */

	public function getAlbum( $album, $what ) {
		
		// define url with api key & album
		$url = self::FLICKR_URL . '?method=flickr.photosets.getPhotos&api_key=' . $this->apikey . '&photoset_id=' . $album . '&extras=url_o,url_t&format=json&nojsoncallback=1';
		
		// get album info
		$album 	= json_decode( file_get_contents( $url ));
		$album	= $album->photoset;
		
		if ( is_array( $what )) {
			foreach ( $what as $info ) {
				if ( $info != 'photo') {
					$result[$info] = $album->$info;
				} else { 
					$result[$info] = $this->pictures( $album->$info );
				}
			}
		} else {
			if ( $info != 'photo') {
				$result[$info] = $album->$info;
			} else { 
				$result[$info] = $this->pictures( $album->$info);
			}
		}
		
		return $result;
		
	}	
	
	
	/**
	 * render pictures 
	 *
	 * @return 	list 
	 * @param 	list 	$nodes
	 */
	 
#'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_m.jpg'
	public function pictures( $nodes, $size = 'm' ) {
	
		$i = 0;
		foreach ($nodes as $node) {
			
			$result[ 'url' ][$i]	= 'http://farm' . $node->farm . '.static.flickr.com/' . $node->server . '/' . $node->id . '_' . $node->secret . '_' . $size . '.jpg';
			$result[ 'title' ][$i] = $node->title;
			
			$i++;
		}
		
		return $result;
	
	}
}
 
 
/** 
 * PICASA CLASS
 *
 * This can be used to communicate with picasa api
 * This is only the beginning of a picasa communication.. should be expanded in the future!
 *
 * @author		Pieter Van Peteghem <pieter@onedotonly.be>
 * @version		version 0.01 beta
 *
 * @copyright	oneDot only <we@onedotonly.be> Copyright (c) 2010, oneDot only. All rights reserved.
 * @license		BSD License
 */



class Picasa {

	CONST PICASA_URL 	= 'http://picasaweb.google.com/data/feed/base';

	
	/**
	 * Default constructor
	 *
	 * @return	void
	 * @param	string $apikey			The consumer key to use.
	 * @param	string $apisecret 		The consumer secret to use.
	 
	 
	 public function __construct($apikey, $apisecret) {
		$this->apikey = $apikey;
		$this->apisecret = $apisecret;
	}*/
	
	
	/**
	 * get album with info
	 *
	 * @return	array 
	 * @param 	user
	 * @param 	albumid
	 */
	 
	public function getAlbum( $user, $album ) {
		
		$xml 	= self::PICASA_URL .  '/user/' . $user . '/albumid/' . $album . '?alt=rss&kind=photo&hl=en_US';
		$list 	= simplexml_load_file($xml);
		foreach ($list->channel->item as $item) {
			$photos[] = $item;
		}	
		return $photos;
	} 
	
	
	/**
	 * render pictures 
	 *
	 * @return 	list 
	 * @param 	user 
	 * @param 	album
	 * @param 	width
	 */
	 
	public function pictures( $user, $album, $width = 250 ) {
	
		$items = $this->getAlbum( $user, $album );
		foreach ($items as $item ) {
			$url = $item->enclosure->attributes()->url;
			$url = $this->getPicture( $url, $width );
			$photos[] = $url;
		}
		
		return $photos;
		
	}



	/**
	 * Get pictures url and resize
	 * 
	 * @return		string
	 * @param		string $url		given url
	 * @param		int $resize		given width/height
	 */
	
	function getPicture ( $url, $resize = 250 ) {
	
		if ( $url )
			$img = explode( '/', $url);
			$img[count($img) - 1] = 's' . $resize . '/' . $img[count($img) - 1];
			$url = implode('/', $img);
	
		return $url;			

	}

	

}